package com.tianma.web.controller.tianma.file;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.tianma.common.core.domain.entity.SysUser;
import com.tianma.common.utils.StringUtils;
import com.tianma.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tianma.common.annotation.Log;
import com.tianma.common.core.controller.BaseController;
import com.tianma.common.core.domain.AjaxResult;
import com.tianma.common.enums.BusinessType;
import org.springframework.web.multipart.MultipartFile;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import com.tianma.web.domain.master.file.WeekEventDetail;
import com.tianma.web.service.master.IWeekEventDetailService;
import com.tianma.common.utils.poi.ExcelUtil;
import com.tianma.common.core.page.TableDataInfo;

/**
 * 周事件明细Controller
 * 
 * @author wwf
 * @date 2025-05-20
 */
@RestController
@RequestMapping("/weekEventDetail/detail")
public class WeekEventDetailController extends BaseController
{
    @Autowired
    private IWeekEventDetailService weekEventDetailService;
    @Autowired
    private SysUserMapper sysUserMapper;


    /**
     * 查询周事件明细列表
     */
    @GetMapping("/list")
    public AjaxResult list(WeekEventDetail weekEventDetail)
    {
        List<WeekEventDetail> list = weekEventDetailService.selectWeekEventDetailListByLike(weekEventDetail);
        for(WeekEventDetail item:list) {
            String nextUserName = item.getNextUserName();
            if (StringUtils.isNotEmpty(nextUserName)) {
                if (StringUtils.isNotEmpty(nextUserName)) {
                    String nickName = weekEventDetailService.processUserHandler(nextUserName);
                    //查找别名
                    item.setRemark(nickName);
                }
            }
        }
        return AjaxResult.success(list);
    }

    /**
     * 导出周事件明细列表
     */
    @Log(title = "周事件明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WeekEventDetail weekEventDetail)
    {
        List<WeekEventDetail> list = weekEventDetailService.selectWeekEventDetailList(weekEventDetail);
        ExcelUtil<WeekEventDetail> util = new ExcelUtil<WeekEventDetail>(WeekEventDetail.class);
        util.exportExcel(response, list, "周事件明细数据");
    }

    /**
     * 获取周事件明细详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(weekEventDetailService.selectWeekEventDetailById(id));
    }

    /**
     * 新增周事件明细
     */
    @Log(title = "周事件明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WeekEventDetail weekEventDetail)
    {
        return toAjax(weekEventDetailService.insertWeekEventDetail(weekEventDetail));
    }

    /**
     * 修改周事件明细
     */
    @Log(title = "周事件明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WeekEventDetail weekEventDetail)
    {
        return toAjax(weekEventDetailService.updateWeekEventDetailNew(weekEventDetail));
    }

    /**
     * 删除周事件明细
     */
    @Log(title = "周事件明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(weekEventDetailService.deleteWeekEventDetailByIds(ids));
    }

    /**
     * 上传周事件明细列表
     */
    @PostMapping("/uploadExcel")
    public AjaxResult uploadWeekEventDetailExcel(MultipartFile file){
    try {
        Workbook workbook=null;
        String filename = file.getOriginalFilename();
        if(filename.contains(".xlsx")) {
            workbook = new XSSFWorkbook(file.getInputStream());
        }else{
            workbook = new HSSFWorkbook(file.getInputStream());
        }
        weekEventDetailService.uploadWeekEventDetailExcel(workbook);
        return AjaxResult.success();
    }catch (Exception e){
        String resultMsg="上传失败";
        e.printStackTrace();
        String message = e.getMessage();
        return AjaxResult.error(resultMsg);
      }
    }
}
